package com.sale.plugins;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.SpringContextLoader;
import com.apache.exception.BusinessException;
import com.apache.funds.common.FundsUtil;
import com.apache.funds.vo.UserFundsVo;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 * 销户验证是否拥有持仓和资金，都没有的情况下可进行销户，
 * 有持仓或者有资金均不能进行销户
 */
public class SaleCancelUserPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        log.info("start----------验证持仓和资金");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("flag", "T");
        resultMap.put("msg", "操作成功");
        String userEname = (String) param.get("uctUser.w_userEname");
        String userCname = (String) param.get("uctUser.w_userCname");
        //获取账户资金
        String userSettleNo = getUserSettleNo(userEname);
        if (StrUtil.isNull(userSettleNo)) {
            return null;
        }
        UserFundsVo userVo = FundsUtil.selectFundsMyMoneyByCache(userSettleNo);
        Long m1 = userVo.getM1();
        Long m2 = userVo.getM2();
        if (m1 > 0 || m2 > 0) {
            log.info("该账号拥有可用资金【" + m1 + "】，冻结资金【" + m2 + "】");
            resultMap.put("flag", "F");
            resultMap.put("msg", "该账号拥有可用资金：" + m1 + "，冻结资金：" + m2 + "。不能执行销户操作");
            throw new BusinessException(JSONArray.fromObject(resultMap).toString());
        }
        //获取持仓信息
        boolean goodsInfo = getGoodsInfo(userEname);
        if (goodsInfo) {
            log.info("该账号【" + userEname + "】拥有持仓。");
            resultMap.put("flag", "F");
            resultMap.put("msg", "该账号【" + userEname + "】拥有持仓。不能执行销户操作");
            throw new BusinessException(JSONArray.fromObject(resultMap).toString());
        }
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 根据登录账号获取资金账号
     *
     * @param createUserEname
     * @return
     */
    private String getUserSettleNo(String createUserEname) {
        ResultEntity ret = FundsUtil.selectFundsMyAccountByCreateUserEname(createUserEname);
        if ("true".equals(ret.getResult()) && ret.getEntity() != null) {
            JSONObject svmUser = (JSONObject) ret.getEntity();
            if (svmUser != null) {
                return String.valueOf(svmUser.get("userName"));
            }
        }
        return null;
    }

    /**
     * 获取持仓信息
     *
     * @param userEname
     * @return
     */
    @SuppressWarnings("rawtypes")
    public boolean getGoodsInfo(String userEname) {
        ProxyManager netProxyManager = (ProxyManager) SpringContextLoader
                .getBean("netProxyManager");
        ParamsVo params = new ParamsVo();
        params.setParams("doCode", "dymicSql");
        params.setParams("sqlKeyId", "ByDymicSql");
        params.setParams("resultType", "objInfo");
        params.setKey("publicForSql");

        //封装参数
        params.setParams("modelTypes", "s_trsGoods");
        params.setParams("trsGoods.w_userEname", userEname);
        ResultEntity dymicSqlParamResult = (ResultEntity) netProxyManager.doInvoke(params);
        List<DataMap> list = (List) dymicSqlParamResult.getEntity();
        if (list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
}
